Ext.ns('TaxonTagger')
TaxonTagger.toolbar = function(config){

	Ext.apply(this, config, {
			id: 'tools'
		,	ttCount: 1	
		,	items: [{
				ref: 'btnExport'
			,	scope: this	
			,	text: this.txtExportBtn
			,	menu: {
					defaults: {
						disabled: true
					}	
				,	items: [{
							text: this.txtDWCBtn
						,	iconCls: 'icon_zip'	
						,	scope: this	
						,	handler: function(){
								this.sendRequest('dwca');
							}
					}, {
							text: this.txtDelimitedBtn
						,	scope: this	
						,	iconCls: 'icon_excel'
						,	handler: function(){
								this.sendRequest('csv');
							}
					}, {
							text: this.txtTextFileBtn
						,	scope: this	
						,	iconCls: 'icon_txt'
						,	handler: function(){
								this.sendRequest('txt');
							}
					}, {
							text: this.txtWebLinkBtn
						,	iconCls: 'icon_weblink'
						,	scope: this	
						,	handler: function(){
								var currDoc = Ext.encode(TaxonTagger.currentDocument) ;
								if (currDoc == 'null'){
									Ext.Msg.alert(this.txtWebLinkBtn, location.href);
								} else {
									Ext.Msg.alert(this.txtWebLinkBtn, location.href + '?document=' + encodeURIComponent(currDoc));
								}	
							}	
					}]
				}
			}, {
					text: this.txtViewBtn
				,	scope: this
				,	ref: 'btnView'
				,	menu: {
						items: [{
								text: this.txtDocumentMode
							,	xtype: 'menucheckitem'
							,	checked: true
							,	ref: 'btnDocumentMode'
							,	group: 'mode'
							,	mode: 'documentMode'
							,	scope: this
							,	checkHandler: this.onModeItemCheck
						}, {
								text: this.txtTaxonMode
							,	checked: false
							,	xtype: 'menucheckitem'
							,	ref: 'btnTaxonMode'
							,	group: 'mode'
							,	mode: 'taxonMode'
							,	scope: this
							,	checkHandler: this.onModeItemCheck
						}]
					}
			}, {
					text: this.txtToolsBtn
				,	scope: this
				,	ref: 'btnTools'	
				,	menu: {
						items: [{
								text: this.txtOptionBtn
							,	iconCls: 'icon_option'	
							,	handler: function(){
									this.settings = new TaxonTagger.settings();
									this.settings.show();
								}
						}]
					}
			}, {
					text: this.txtHelpBtn
				,	menu: {
						items: [{
								text: this.txtAboutBtn
							,	iconCls: 'icon_information'
							,	scope: this
							,	handler: function() {									
									Ext.Msg.alert(this.alertAboutTitle, this.alertAboutmsg);
								}
						}, {
								text: this.txGetStartBtn
							,	iconCls: 'icon_page'	
							,	handler: function() { 
									window.open('http://code.google.com/p/taxontagger/wiki/Quickstart', '_blank');
								}
						}, {
								text: this.txtProWebBtn
							,	iconCls: 'icon_page'		
							,	handler: function() { 
									window.open('http://code.google.com/p/taxontagger/', '_blank');
								}
						}, {
								text: this.txtReportIssueBtn
							,	iconCls: 'icon_page'
							,	scope: this	
							,	handler: function() { 
									window.open(Config.issuesUrl, '_blank');
								}
						}, {
								text: this.txtTutorialBtn
							,	scope: this	
							,	hidden: true
							,	iconCls: 'icon_tutorial'
							,	handler: function(){
									Ext.Msg.alert('Tutorial',Config.tutorial1.initialMsg ,function(){
										var tutorial = new TaxonTagger.Tutorial();
										tutorial.tutCount = 0;
										tutorial.setTutorial(0);
									}, this);
								}	
						}]
					}
			}, '->', {
					xtype: 'label'
				,	text: this.txtLanCombo
			}, {
					xtype: 'combo'   
				,	store: language
				,	ref:  'cmbLang'	
				,	mode: 'local'
				,	value: lang
				,	displayField: 'value'
				,	valueField: 'id'
				,	typeAhead: false
				,	triggerAction: 'all'
				,	editable: false
				,	width: 100
				,	listeners: {
						select: function(f, r, i){
							var lang = r.data.id;
							window.location ='?lang=' + lang;
						}
					}
			}]
	});

	TaxonTagger.toolbar.superclass.constructor.call(this, config);
	
};

Ext.extend(TaxonTagger.toolbar, Ext.Toolbar,{
		
		sendRequest: function(type){
			var tConfig = Ext.decode(localStorage.getItem("settings")) || Config;
			var fileSettings;
			if (tConfig["delimiter"]["csvStorage"]) fileSettings = tConfig["delimiter"]["setCsv"];
			if (tConfig["delimiter"]["tabStorage"]) fileSettings = tConfig["delimiter"]["setTab"];
			if (tConfig["delimiter"]["customStorage"]) fileSettings = tConfig["delimiter"]["setCustom"];
			
			var params = {
					type: type	
				,	list: this.listType(type)
				,	info: Ext.encode(TaxonTagger.currentDocument.info)
				,	customWords: TaxonTagger.currentDocument.customWords
				,	removedWords: TaxonTagger.currentDocument.removedWords
				,	comments: TaxonTagger.currentDocument.comments
				,	fileSettings: Ext.encode(fileSettings)
			}

			Ext.each(Ext.select('form').elements, function(item){
				if(item.action == Config.baseUrl+'resources/api/export.php?'){
					item.parentNode.removeChild(item)
				}
			},this)
			
			TaxonTagger.Notice.msg(this.msgTitle,  this.msgDownload);
			this.post_to_url('resources/api/export.php?' , params, 'POST')
		}

	,	listType: function(type){
			var dwcalist = [];
			var txtlist = '';
			var csvlist = [];
			TaxonTagger.docStore.allData.each(function( item ) {
				txtlist += item.data.scientificName + "<br>";
			//	csvlist += item.data.scientificName + "|" + item.data.source + "|" + item.data.comment+ "<br>";
				csvlist.push(item.data);
				dwcalist.push(item.data);
			});

			switch(type){
				case 'dwca' : 
					return Ext.encode(dwcalist);
					break;

				case 'csv' : 
					return Ext.encode(csvlist);
					break;

				case 'txt' : 
					return txtlist;
					break;

				default:
					break;		
			}
		}
	
	,	enableChild: function(){
			this.btnExport.menu.items.each(function(item){
				if (item.disabled) {
					item.enable();
				}
			}, this);
		}

	,	onModeItemCheck: function(btn, status){
			if (status) {
				this.fireEvent('modeChanged', btn.mode, this);
			}
		}

	,	post_to_url: function(path, params, method) {
			method = method || "post";
		
			var form = document.createElement("form");
			form.setAttribute("method", method);
			form.setAttribute("action", path);
		
			for(var key in params) {
				var hiddenField = document.createElement("input");
				hiddenField.setAttribute("type", "hidden");
				hiddenField.setAttribute("name", key);
				hiddenField.setAttribute("value", params[key]);
	
				form.appendChild(hiddenField);
			}
			document.body.appendChild(form);    
			form.submit();
		}

});